﻿function okGrowl(title, message, timeout) {
    $("div.okGrowlUI").empty();
    if (title) $("div.okGrowlUI").append('<h2>' + title + '</h2>');
    if (message) $("div.okGrowlUI").append('<h3>' + message + '</h3>');
    if (timeout === undefined) timeout = 3000;

    $.blockUI({
        message: $("div.okGrowlUI"),
        fadeIn: 700,
        fadeOut: 700,
        timeout: timeout,
        showOverlay: false,
        centerY: false,
        centerX: false,
        css: {
            width: '350px',
            top: '0px',
            left: '0%',
            position: 'fixed',
            right: '',
            border: 'none',
            padding: '5px',
            backgroundColor: '#000',
            '-webkit-border-radius': '10px',
            '-moz-border-radius': '10px',
            opacity: .6,
            color: '#fff'
        }
    });
}

function spinblock() {
    $.blockUI({
        message:
            //'<br /><img src="/images/loadingAnimation.gif" /><h2> Please wait...</h2>',
            '<br /><img alt="" src="" /><h2> Please wait...</h2>',
        fadeIn: 500
    });
}

var fileDownloadCheckTimer;
function downloadFile() {
    spinblock();
    var token = new Date().getTime();
    $('#downloadTokenId').val(token); //use the current timestamp as the token value
    $('#downloadForm').submit();

    //console.log("Token value: " + token);
    fileDownloadCheckTimer = window.setInterval(function () {
        var cookieValue = $.cookie('fileDownloadToken');
        if (cookieValue == token) {
            finishDownload();
        } else {
            // console.log("Cookie value: " + cookieValue);
        }
    }, 1000);
}

function finishDownload() {
    window.clearInterval(fileDownloadCheckTimer);
    $.cookie('fileDownloadToken', null); //clears the cookie value
    $.unblockUI();
}